package launch

import (
	_ "github.com/lib/pq"
	"log"
	"shop/src/model"
	"xorm.io/xorm"
)

var tables = [3]interface{}{
	new(model.Order),
}

func Xorm() *xorm.Engine {
	engine, err := xorm.NewEngine("postgres", "postgres://postgres:postgres@localhost/postgres?sslmode=disable")
	if err != nil {
		log.Fatal("连接数据库失败" + err.Error())
	}
	for table := range tables {
		exist, err := engine.IsTableExist(table)
		if err != nil {
			log.Fatalln("检查表错误" + err.Error())
		}
		if !exist {
			err = engine.CreateTables(new(model.Order))
			if err != nil {
				log.Fatalln("创建表失败" + err.Error())
			}
		}
	}
	return engine
}
